import { getContract } from './utils.js'

import ABI_airdrop from '../constants/abis/airdrop.json'
import ABI_erc20 from '../constants/abis/erc20.json'
import ABI_getsize from '../constants/abis/getsize.json'
import ABI_upload from '../constants/abis/upload.json'

// returns null on errors
function useContract (
  library,
  account,
  address,
  ABI,
  withSignerIfPossible = true,
) {
  if (!address || !ABI || !library) return null
  try {
    // return new web3.eth.Contract(ABI,address)
    return getContract(
      address,
      ABI,
      library,
      withSignerIfPossible && account ? account : undefined,
    )
  } catch (error) {
    console.error('Failed to get contract', error)

    return null
  }
}

export function useABI_erc20_Contract (
  library,
  account,
  tokenAddress,
  withSignerIfPossible,
) {
  return useContract(
    library,
    account,
    tokenAddress,
    ABI_erc20,
    withSignerIfPossible,
  )
}

export function useABI_airdrop_Cpntract (
  library,
  account,
  tokenAddress,
  withSignerIfPossible,
) {
  return useContract(
    library,
    account,
    tokenAddress,
    ABI_airdrop,
    withSignerIfPossible,
  )
}

export function useABI_upload_Contract (
  library,
  account,
  tokenAddress,
  withSignerIfPossible,
) {
  return useContract(
    library,
    account,
    tokenAddress,
    ABI_upload,
    withSignerIfPossible,
  )
}

export function useABI_getsize_Contract (
  library,
  account,
  tokenAddress,
  withSignerIfPossible,
) {
  return useContract(
    library,
    account,
    tokenAddress,
    ABI_getsize,
    withSignerIfPossible,
  )
}
